package mundo;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class mundo {

	private static String idec;
	
	private static String ecua;
	
	private static String incog;
	
	private static Nodo nodo;
	
	
	private static int lim;
	
	/**
	 * @param args
	 * @throws FileNotFoundException 
	 */
	public static void main(String[] args) throws FileNotFoundException {
		// TODO Auto-generated method stub
		
		File f = new File( "./docs/equ.in" );
	    BufferedReader entrada = new BufferedReader( new FileReader( f ) );
	    if ( f.exists() )
			try {
				
				idec = entrada.readLine();
				ecua = entrada.readLine();
				incog = entrada.readLine();
				lim = Integer.parseInt(entrada.readLine()); 
				System.out.println(idec);
				System.out.println(ecua);
				System.out.println(incog);
				System.out.println(lim);
				
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		System.out.println("--------------------");
		System.out.println("[0] " + ecua);
		resolverEcua(ecua, lim);
		

	}
	
	public static void resolverEcua(String ecuacion, int limite){
	
		String [] listaEcuacion = ecuacion.split(" ");
		
		armarArbol(listaEcuacion);	
		
		
	}
	
	
	public static void armarArbol(String[] laEcuacion)
	{
		
		nodo = new Nodo(laEcuacion[laEcuacion.length-1]);
		 
			recursion(nodo, laEcuacion, laEcuacion.length-2);	
		    
			
		
	}
	
	public static void recursion(Nodo nod, String[] ecuacion, int pos )
	{
		if(pos>=0)
		{
               Nodo aux = new Nodo(ecuacion[pos]);
               if(nod.noHayHijoD())
               nod.agregarHijoD(aux);
               
               else
            	   nod.agregarHijoI(aux);
		
               if(aux.esOperadorSimple())
            	   recursion(aux,ecuacion, pos-1);
               
               if(aux.esOperadorNoSimple())
            	   recursion(aux,ecuacion, pos-1);
               
               else
            	   recursion(nod.darhijoI(),ecuacion, pos-1);
		}
            	   		
	}
}
